package com.example.personalinfoapp;

import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;

import androidx.appcompat.app.AppCompatActivity;

public class InterestSelectionActivity extends AppCompatActivity {

    private CheckBox cbReading, cbPhotography, cbPingpong;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_interest_selection);

        // 初始化视图
        cbReading = findViewById(R.id.cb_reading);
        cbPhotography = findViewById(R.id.cb_photography);
        cbPingpong = findViewById(R.id.cb_pingpong);

        // 返回按钮
        Button btnBack = findViewById(R.id.btn_back);
        btnBack.setOnClickListener(v -> finish());

        // 设置返回按钮的点击反馈
        btnBack.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setAlpha(0.5f);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        v.setAlpha(1.0f);
                        break;
                }
                return false;
            }
        });

        // 确定按钮
        Button btnConfirm = findViewById(R.id.btn_confirm);
        btnConfirm.setOnClickListener(v -> {
            // 收集选中的兴趣
            StringBuilder selectedInterests = new StringBuilder();

            if (cbReading.isChecked()) {
                selectedInterests.append("阅读");
            }

            if (cbPhotography.isChecked()) {
                if (selectedInterests.length() > 0) {
                    selectedInterests.append(",");
                }
                selectedInterests.append("摄影");
            }

            if (cbPingpong.isChecked()) {
                if (selectedInterests.length() > 0) {
                    selectedInterests.append(",");
                }
                selectedInterests.append("乒乓球");
            }

            // 返回结果
            Intent resultIntent = new Intent();
            resultIntent.putExtra("selectedInterests", selectedInterests.toString());
            setResult(RESULT_OK, resultIntent);
            finish();
        });

        // 设置确定按钮的点击反馈
        btnConfirm.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        v.setAlpha(0.5f);
                        break;
                    case MotionEvent.ACTION_UP:
                    case MotionEvent.ACTION_CANCEL:
                        v.setAlpha(1.0f);
                        break;
                }
                return false;
            }
        });
    }
}